/**
 * @file global.h     
 * @brief 
 * @author henry.cai (henry.cai@queclink.com)
 * @version 1.0
 * @date 2024-09-04
 * @defgroup application
 * 
 * @copyright Copyright (C) 2019 Queclink Wireless Solutions (ShenZhen) Co., Ltd. All Rights Reserved
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2024-09-04 <td>1.0     <td>henry.cai     <td>全局定义的文件头
 * </table>
 */

#ifndef __GLOBAL_H
#define __GLOBAL_H

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include "public/errno.h"


#ifdef __cplusplus
extern "C" {
#endif

#ifndef _SSIZE_T
#define _SSIZE_T
typedef signed int ssize_t;
#endif


#include "cmsis_os2.h"
#ifndef qlmalloc
#define qlmalloc(size)     pvPortMalloc(size)
#define qlfree(ptr)        vPortFree(ptr)
#endif

#ifndef qlwdog
#define qlwdog()           bsp_wdt_feed()
#endif


/* task priority list
 * the configMAX_PRIORITIES is 56 */
#define TASKPRI_MMGR        osPriorityNormal1
#define TASKPRI_SERVICE     osPriorityNormal4       /* 需要管理socket开关, 注意优先级需要低于Modem_FSM任务 */
#define TASKPRI_COCOMM      osPriorityNormal3
#define TASKPRI_GNSS        osPriorityNormal2
#define TASKPRI_SENSOR      osPriorityNormal2
#define TASKPRI_MISC        osPriorityNormal7       /* 管理关机, 需要最高优先级 */
#define TASKPRI_SHELL       osPriorityNormal7
#define TASKPRI_LOWPOWER    osPriorityBelowNormal7  /* 管理设备休眠, 需要其他任务都跑完才能执行 */
#define TASKPRI_BUSSINESS   osPriorityNormal        /* 负责硬件喂狗 */

/* task stack size list */
#define TASKSTACK_MMGR      (1536)
#define TASKSTACK_SERVICE   (2048)
#define TASKSTACK_COCOMM    (1024)
#define TASKSTACK_GNSS      (2048)
#define TASKSTACK_SENSOR    (1024)
#define TASKSTACK_MISC      (1024)
#define TASKSTACK_SHELL     (1024)
#define TASKSTACK_LOWPOWER  (2048)
#define TASKSTACK_BUSSINESS (1024)


#ifndef SN_MAX_SIZE
#define SN_MAX_SIZE         (32)    /*!< SN最大长度 */
#endif

#ifndef APN_MAX_SIZE
#define APN_MAX_SIZE        (32)    /*!< APN Name最大长度 */
#define USER_MAX_SIZE       (32)    /*!< APN user最大长度 */
#define PWD_MAX_SIZE        (32)    /*!< APN password最大长度 */
#endif

#define SUPPORT_FOR_WIFI_UPLOAD (0) /* 1 - 支持WIFI上报， 0 - 禁止WIFI上报 */

/**
 * @brief APN关键参数结构体
 */
typedef struct
{
    char name[APN_MAX_SIZE];
    char user[USER_MAX_SIZE];
    char pwd[PWD_MAX_SIZE];
} apn_param_t;


/**
 * @brief log关键参数结构体
 */
typedef struct
{
    uint8_t enable;
    uint8_t level;
    char filter_tag[16];
} log_params_t;


typedef struct {
    uint8_t         magic;  /* 0x55 magic number, indicate the server pararm is available or not */ 
    bool            enable;
    char            ipaddr[128];
    uint16_t        port;
    uint8_t         is_ssl;
    char            ping_addr[80];
} server_params_t;


typedef struct {
    uint8_t         magic;  /* 0x55 magic number, indicate the server pararm is available or not */ 
    uint16_t        cacert_len;
    char            cacert[2048];
} server_ssl_params_t;

#ifndef IMEI_MAX_SZIE
#define IMEI_MAX_SZIE       (16)
#endif

#define TAG_BSP    "BSP"
#define TAG_MAIN   "MAIN"
#define TAG_MODEM  "MODEM"
#define TAG_AADL   "AADL"
#define TAG_HDL    "HDL"
#define TAG_GNSS   "GNSS"
#define TAG_SNSR   "SNSR"
#define TAG_TBOX   "TBOX"
#define TAG_SHELL  "SHELL"
#define TAG_MISC   "MISC"
#define TAG_LP     "LOWPOWER"
#define TAG_QLIBC  "QLIBC"
#define TAG_COMCU  "COMCU"

/*
 * FOTA string mark,
 * IDLE --> STRT --> REDY --> PASS/FAIL --> IDLE
 */
#define FOTA_OK_STR    "PASS" /* 固件更新成功, 只能由 bootloader 置位 */
#define FOTA_ERR_STR   "FAIL" /* 固件更新失败, 只能由 bootloader 置位 */
#define FOTA_RDY_STR   "REDY" /* 固件下载成功, 等待升级, 只能由 APP 置位 */
#define FOTA_IDLE_STR  "IDLE" /* 空闲, 没有固件升级业务, 只能由 APP 置位 */
#define FOTA_START_STR "STRT" /* 开始下载固件, 只能由 APP 置位 */

/**
 * @brief 全局错误处理函数，error code refer to @errno.h
 */
extern void Error_Handler(void);
extern int32_t sailvan_bussiness_init(void);


#define assert_ptr(ptr) 
#define assert_ptr_ret(ptr, ret)  
#define assert_expr(expr)               /*  @brief 断言表达式, 如果表达式为假, 则返回 */
#define assert_expr_ret(expr, ret)      /*  @brief 断言表达式, 如果表达式为假, 则返回错误码 @ret */

#ifdef __cplusplus
}
#endif

#endif // __GLOBAL_H